package com.weiyi.mashangzhu.Roomer.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Adapter.AddressAdapter;
import com.weiyi.mashangzhu.Roomer.Adapter.LandMarkListAdapter;
import com.weiyi.mashangzhu.Roomer.Bean.AddressBean;
import com.weiyi.mashangzhu.Roomer.Bean.SearchLandMarkBean;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.weiyi.mashangzhu.Weight.MyListView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Addressctivity extends BaseActivity {

    @BindView(R.id.lv1)
    ListView lv1;
    @BindView(R.id.lv2)
    ListView lv2;
    @BindView(R.id.lv3)
    ListView lv3;
    List<String> list_lv1 = new ArrayList<>();
    String city = "";
    int type;
    List<String> list_lv2 = new ArrayList<>();
    String ad = "";
    @BindView(R.id.layout_lv2)
    RelativeLayout layoutLv2;
    @BindView(R.id.lv_landmark)
    MyListView lvLandmark;
    String select = "";
    @BindView(R.id.edtext)
    EditText edtext;
    @BindView(R.id.layout_searchresult)
    RelativeLayout layoutSearchresult;
    private TextView name,name2;
    int currentItem1 = 0; //当前被选中的item位置`
    int currentItem2 = 0; //当前被选中的item位置`
    AddressAdapter addressAdapter1=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addressctivity);
        ButterKnife.bind(this);
        city = getIntent().getStringExtra("city");
        initEditText();
        initlv1();
        OkHttpUtils.get().url(API.BaseApi_R + API.Address_Select_Api).addParams("token", SPUtils.get(Addressctivity.this, "token", "") + "")
                .addParams("cityname", city)
                .addParams("adname", "")
                .addParams("types", 1 + "")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        T.showShort(Addressctivity.this, "网络连接失败，请检查一下网络设置");
                    }

                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        AddressBean addressbean = gson.fromJson(response, AddressBean.class);
                        list_lv2.clear();
                        list_lv2 = addressbean.getData().getAdnamelist();
                        ad = list_lv2.get(0);
                        initlv2(list_lv2);
                        OkHttpUtils.get().url(API.BaseApi_R + API.Address_Select_Api).addParams("token", SPUtils.get(Addressctivity.this, "token", "") + "")
                                .addParams("cityname", city)
                                .addParams("adname", list_lv2.get(0))
                                .addParams("types", 1 + "")
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Request request, Exception e) {
                                        T.showShort(Addressctivity.this, "网络连接失败，请检查一下网络设置");
                                    }

                                    @Override
                                    public void onResponse(String response) {
                                        Gson gson = new Gson();
                                        AddressBean addressbean = gson.fromJson(response, AddressBean.class);

                                        List<String> list_lv3 = new ArrayList<String>();
                                        list_lv3.add("全部");
                                        list_lv3.addAll(addressbean.getData().getMarklist());
                                        lv3.setAdapter(new AddressAdapter(list_lv3, Addressctivity.this, "no", 3,-1));

                                    }
                                });
                    }
                });
        lv3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView name3 = (TextView) view.findViewById(R.id.name);
                String s = "";
                if ((name3.getText()).equals("全部")) {
                    s = ad;
                } else {
                    s = name3.getText() + "";
                }
                Intent intent = new Intent();
                intent.putExtra("address", s);
                setResult(0x002, intent);
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void initEditText() {
        edtext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //当输入框里面的值为空，更新为原来的列表，否则为过滤数据列表
                filterData(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void filterData(String string) {
        if (TextUtils.isEmpty(string)) {
            layoutSearchresult.setVisibility(View.GONE);
        } else {
            layoutSearchresult.setVisibility(View.VISIBLE);
            OkHttpUtils.get().addParams("token", SPUtils.get(this, "token", "") + "").addParams("cityname", city)
                    .addParams("keyword", string)
                    .url(API.BaseApi_R + API.SearchLandmark_Api).build().execute(new StringCallback() {
                @Override
                public void onError(Request request, Exception e) {
                    T.showShort(Addressctivity.this, "网络连接失败，请检查一下网络设置");
                }

                @Override
                public void onResponse(String response) {
                    SearchLandMarkBean searchlandmarkbean = new Gson().fromJson(response, SearchLandMarkBean.class);
                    initLandmark(searchlandmarkbean);


                }
            });


        }
    }

    //地标列表
    private void initLandmark(SearchLandMarkBean searchlandmarkbean) {
        final List<String> marklist = searchlandmarkbean.getData().getMarklist();
        Log.e("----------", "==" + marklist.size());
        if (marklist.size() == 0) {
            layoutSearchresult.setVisibility(View.GONE);
        } else {

            lvLandmark.setAdapter(new LandMarkListAdapter(marklist, Addressctivity.this, "img_no"));
            layoutSearchresult.setVisibility(View.VISIBLE);
        }
        lvLandmark.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                select = marklist.get(position);
                Intent intent = new Intent();
                intent.putExtra("address", select);
                setResult(0x002, intent);
                finish();
            }
        });

    }

    private void initlv2(final List<String> list) {
        lv2.setAdapter(new AddressAdapter(list, this, "yes", 2,currentItem2));
        lv2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (position != 0) {
                    parent.getChildAt(0).findViewById(R.id.name).setBackgroundColor(getResources().getColor(R.color.light_gray_face));
                }
                if (name2 != null) {
                    name2.setBackgroundResource(R.color.light_gray_face);
                }
                name2 = (TextView) view.findViewById(R.id.name);
                name2.setBackgroundResource(R.color.house_detail_cyan);

                OkHttpUtils.get().url(API.BaseApi_R + API.Address_Select_Api).addParams("token", SPUtils.get(Addressctivity.this, "token", "") + "")
                        .addParams("cityname", city)
                        .addParams("adname", list.get(position))
                        .addParams("types", 1 + "")
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Request request, Exception e) {
                                T.showShort(Addressctivity.this, "网络连接失败，请检查一下网络设置");
                            }

                            @Override
                            public void onResponse(String response) {
                                Gson gson = new Gson();
                                AddressBean addressbean = gson.fromJson(response, AddressBean.class);
                                List<String> list_lv3 = new ArrayList<String>();
                                list_lv3.add("全部");
                                list_lv3.addAll(addressbean.getData().getMarklist());
                                lv3.setAdapter(new AddressAdapter(list_lv3, Addressctivity.this, "no", 3,-1));

                            }
                        });
            }
        });

    }

    private void initlv1() {
        list_lv1.add("行政区");
        list_lv1.add("车站/机场");
        list_lv1.add("地铁站");
        list_lv1.add("商圈");
        list_lv1.add("景点");
        list_lv1.add("高校");
        list_lv1.add("医院");
        addressAdapter1=new AddressAdapter(list_lv1, this, "yes", 1,currentItem1);
//        lv1.setAdapter(addressAdapter1);
        lv1.setAdapter(addressAdapter1);
        lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position != 0) {
                    parent.getChildAt(0).findViewById(R.id.name).setBackgroundColor(getResources().getColor(R.color.white));
                }
                if (name != null) {
                    name.setBackgroundResource(R.color.white);
                }
                name = (TextView) view.findViewById(R.id.name);
                name.setBackgroundResource(R.color.light_gray_face);
//                TextView name = (TextView) view.findViewById(R.id.name);
//                currentItem1=position;
//                addressAdapter1.notifyDataSetChanged();
                if (!city.equals("")) {
                    if ((name.getText()).equals("行政区")) {
                        type = 1;
                    } else if ((name.getText()).equals("车站/机场")) {
                        type = 2;
                    } else if ((name.getText()).equals("地铁站")) {
                        type = 3;
                    } else if ((name.getText()).equals("商圈")) {
                        type = 4;
                    } else if ((name.getText()).equals("景点")) {
                        type = 5;
                    } else if ((name.getText()).equals("高校")) {
                        type = 6;
                    } else if ((name.getText()).equals("医院")) {
                        type = 7;
                    }
                }
                OkHttpUtils.get().url(API.BaseApi_R + API.Address_Select_Api).addParams("token", SPUtils.get(Addressctivity.this, "token", "") + "")
                        .addParams("cityname", city)
                        .addParams("adname", "")
                        .addParams("types", type + "")
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Request request, Exception e) {
                                T.showShort(Addressctivity.this, "网络连接失败，请检查一下网络设置");
                            }

                            @Override
                            public void onResponse(String response) {
                                Gson gson = new Gson();
                                AddressBean addressbean = gson.fromJson(response, AddressBean.class);
                                list_lv2.clear();
                                list_lv2 = addressbean.getData().getAdnamelist();
                                List<String> list_lv3 = new ArrayList<String>();
                                list_lv3.add("全部");
                                list_lv3.addAll(addressbean.getData().getMarklist());
                                switch (type) {
                                    case 1:
                                        layoutLv2.setVisibility(View.VISIBLE);
//                                        lv2.setVisibility(View.VISIBLE);
                                        initlv2(list_lv2);
                                        break;
                                    case 2:
                                        layoutLv2.setVisibility(View.GONE);
                                        lv3.setAdapter(new AddressAdapter(list_lv3, Addressctivity.this, "no", 3,-1));
                                        break;
                                    case 3:
                                        layoutLv2.setVisibility(View.GONE);
                                        lv3.setAdapter(new AddressAdapter(list_lv3, Addressctivity.this, "no", 3,-1));
                                        break;
                                    case 4:
                                        layoutLv2.setVisibility(View.GONE);
                                        lv3.setAdapter(new AddressAdapter(list_lv3, Addressctivity.this, "no", 3,-1));
                                        break;
                                    case 5:
                                        layoutLv2.setVisibility(View.GONE);
                                        lv3.setAdapter(new AddressAdapter(list_lv3, Addressctivity.this, "no", 3,-1));
                                        break;
                                    case 6:
                                        layoutLv2.setVisibility(View.GONE);
                                        lv3.setAdapter(new AddressAdapter(list_lv3, Addressctivity.this, "no", 3,-1));
                                        break;
                                    case 7:
                                        layoutLv2.setVisibility(View.GONE);
                                        lv3.setAdapter(new AddressAdapter(list_lv3, Addressctivity.this, "no", 3,-1));
                                        break;
                                }


                            }
                        });

            }
        });

    }

    @OnClick({R.id.back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }
}
